package com.yyq.adms.usermanage.bpo.impl;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.gson.Gson;
import com.yyq.adms.usermanage.bpo.SysDeptsBpo;
import com.yyq.adms.usermanage.dao.SysDeptsDao;
import com.yyq.adms.usermanage.pojo.SysDepts;
import com.yyq.sos.pojo.TreeNodeEntity;
import com.yyq.sos.service.BaseBpoImpl;

@Service("sysDeptsBpo")
@Transactional
public class SysDeptsBpoImpl extends BaseBpoImpl implements SysDeptsBpo {

	private SysDeptsDao dao;
	
	@Resource(name="sysDeptsDao")
	public void setDao(SysDeptsDao dao) {
		this.dao = dao;
		super.setBaseDao(dao);
	}

	@Transactional(readOnly = true)
	public List list(String path) {
		FileOutputStream fileOutputStream = null;
		try
		{
			fileOutputStream = new FileOutputStream(path
					+ "/usermanage/dept/tree.xml");
			fileOutputStream.write(getTree().getBytes());
			fileOutputStream.flush();
			fileOutputStream.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 创建模块列表树DOM，返回DOM字符串
	 * @return
	 */
	@Transactional(readOnly = true)
	public String getTree()
	{
		List<TreeNodeEntity> treeNodes = new ArrayList<TreeNodeEntity>();
		TreeNodeEntity parent = new TreeNodeEntity();
		parent.setId(0);
		parent.setPId(-1);
		parent.setName("组织机构");
		parent.setClick(false);
		parent.setOpen(true);
		treeNodes.add(parent);
		List list = dao.list("from "+SysDepts.class.getName()+" o order by o.parentId asc,o.deptType asc");
		for(int i=0; i<list.size(); ++i)
		{
			SysDepts obj = (SysDepts)list.get(i);
			TreeNodeEntity child = new TreeNodeEntity();
			child.setId(obj.getId().intValue());
			child.setPId(obj.getParentId().intValue());
			child.setName(obj.getDeptCname());
			child.setTitle(obj.getDeptName());
			treeNodes.add(child);
		}
		Gson gson = new Gson();
		return gson.toJson(treeNodes);
	}
	
	/**
	 * 创建模块列表树的子节点
	 * @param obj
	 * @return
	 */
	private Element createChildNode(SysDepts obj)
	{
		Element node = new DOMElement("item");
		node.addAttribute("text", obj.getDeptCname());
		node.addAttribute("id", String.valueOf(obj.getId()));
		node.addAttribute("tooltip", obj.getDeptName());
		node.addAttribute("open", "1");
		if(obj.getDeptType().equals("0"))
			node.addAttribute("im0", "magazines_close.gif");
		else
			node.addAttribute("im0", "books_close.gif");
		node.addAttribute("im1", "books_close.gif");
		node.addAttribute("im2", "tombs.gif");
		return node;
	}
	
	/**
	 * 创建模块列表树的父节点
	 * @param obj
	 * @return
	 */
	private Element createParentNode(Document doc)
	{	
		doc.setXMLEncoding("GBK");
		Element root=doc.addElement("tree");
		root.addAttribute("id", "-1");
		Element tree = new DOMElement("item");
		tree.addAttribute("text", "广发银行组织机构");
		tree.addAttribute("id", "0");
		tree.addAttribute("tooltip", "广发银行组织机构");
		tree.addAttribute("open", "1");
		tree.addAttribute("im0", "books_close.gif");
		tree.addAttribute("im1", "tombs.gif");
		tree.addAttribute("im2", "tombs.gif");
		tree.addAttribute("call", "1");
		tree.addAttribute("select", "1");
		root.add(tree);
		return tree;
	}
}
